<?php
/**
 * 热门浏览组件
 */
namespace frontend\widgets\hot;

use common\models\PostExtendModel;
use common\models\PostModel;
use Yii;
use yii\base\Widget;
use yii\db\Query;

class HotWidget extends Widget
{
    public $title = '';
    public $limit = 10;

    public function run()
    {
        $res = (new Query())
            ->select('a.browser, b.title, b.id')
            ->from(['a' => PostExtendModel::tableName()])
            ->join('LEFT JOIN', ['b' => PostModel::tableName()], 'a.post_id = b.id')
            ->where(['b.is_valid' => PostModel::IS_VALID])
            ->orderBy(['a.browser' => SORT_DESC, 'b.id' => SORT_DESC])
            ->limit($this->limit)
            ->all();
        $result['title'] = $this->title ? $this->title : '热门浏览';
        $result['body'] = $res ? $res : [];

        return $this->render('index', ['data' => $result]);
    }
}